package com.jlt.dao.account;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springside.modules.orm.hibernate.HibernateDao;

import com.jlt.entity.account.Post;
import com.jlt.entity.account.User;

/**
 * 职位对象的泛型DAO.
 * 
 * @author zhoutao
 */
@Component
public class PostDao extends HibernateDao<Post, Long> {

	private static final String QUERY_USER_BY_POSTID = "select u from User u left join u.postSet r where r.id=?";

	/**
	 * 重载函数,因为Post中没有建立与User的关联,因此需要以较低效率的方式进行删除User与Post的多对多中间表.
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void delete(Long id) {
		Post post = get(id);
		//查询出拥有该职位的用户,并删除该用户的职位.
		List<User> users = createQuery(QUERY_USER_BY_POSTID, post.getId()).list();
		for (User u : users) {
			u.getPostSet().remove(post);
		}
		super.delete(post);
	}
}
